1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#![allow(non_camel_case_types)]

use std::mem::ManuallyDrop;

use crate::co;
use crate::decl::*;

/// This trait is enabled with the `oleaut` feature, and provides common methods
/// for [`VARIANT`](crate::VARIANT) and [`PROPVARIANT`](crate::PROPVARIANT).
///
/// Prefer importing this trait through the prelude:
///
/// ```no_run
/// use winsafe::prelude::*;
/// ```
pub trait oleaut_Variant: Default {
	/// Returns a reference to the raw data being held.
	///
	/// This method can be used as an escape hatch to interoperate with other
	/// libraries.
	#[must_use]
	fn raw(&self) -> &[u8; 16];

	/// Creates an object straight from raw data. Up to 16 `u8` elements will be
	/// actually copied.
	///
	/// This method can be used as an escape hatch to interoperate with other
	/// libraries.
	///
	/// # Safety
	///
	/// Be sure the binary data is correct.
	#[must_use]
	unsafe fn from_raw(vt: co::VT, data: &[u8]) -> Self;

	/// Returns the [`co::VT`](crate::co::VT) variant type currently being held.
	#[must_use]
	fn vt(&self) -> co::VT;

	/// Tells whether no value is being held, that is, the variant type is
	/// [`co::VT::EMPTY`](crate::co::VT::EMPTY).
	#[must_use]
	fn is_empty(&self) -> bool {
		self.vt() == co::VT::EMPTY
	}

	/// Tells whether the object holds an SQL style null, that is, the variant
	/// type is [`co::VT::NULL`](crate::co::VT::NULL).
	#[must_use]
	fn is_null(&self) -> bool {
		self.vt() == co::VT::NULL
	}

	/// Crates a new object holding a `bool` value.
	#[must_use]
	fn new_bool(val: bool) -> Self
		where Self: Sized,
	{
		let val16: i16 = if val { -1 } else { 0 };
		unsafe { Self::from_raw(co::VT::BOOL, &val16.to_ne_bytes()) }
	}

	/// If the object holds a `bool` value, returns it, otherwise `None`.
	#[must_use]
	fn bool(&self) -> Option<bool> {
		if self.vt() == co::VT::BOOL {
			let val16 = i16::from_ne_bytes(self.raw()[..2].try_into().unwrap());
			Some(val16 != 0)
		} else {
			None
		}
	}

	/// Creates a new object holding a [`BSTR`](crate::BSTR) value.
	#[must_use]
	fn new_bstr(val: &str) -> HrResult<Self>
		where Self: Sized,
	{
		let mut bstr = BSTR::SysAllocString(val)?;
		let ptr = bstr.leak() as usize;
		Ok(unsafe { Self::from_raw(co::VT::BSTR, &ptr.to_ne_bytes()) })
	}

	/// If the object holds a [`BSTR`](crate::BSTR) value, returns it, otherwise
	/// `None`.
	#[must_use]
	fn bstr(&self) -> Option<String> {
		if self.vt() == co::VT::BSTR {
			let ptr = usize::from_ne_bytes(self.raw()[..8].try_into().unwrap());
			let bstr = ManuallyDrop::new(unsafe { BSTR::from_ptr(ptr as _) }); // won't release the stored pointer
			Some(bstr.to_string())
		} else {
			None
		}
	}

	/// Creates a new `VARIANT` holding an `f32` value.
	#[must_use]
	fn new_f32(val: f32) -> Self
		where Self: Sized,
	{
		unsafe { Self::from_raw(co::VT::R4, &val.to_ne_bytes()) }
	}

	/// If the `VARIANT` holds an `f32` value, returns it, otherwise `None`.
	#[must_use]
	fn f32(&self) -> Option<f32> {
		if self.vt() == co::VT::R4 {
			Some(f32::from_ne_bytes(self.raw()[..4].try_into().unwrap()))
		} else {
			None
		}
	}

	/// Creates a new object holding an `f64` value.
	#[must_use]
	fn new_f64(val: f64) -> Self
		where Self: Sized,
	{
		unsafe { Self::from_raw(co::VT::R8, &val.to_ne_bytes()) }
	}

	/// If the object holds an `f64` value, returns it, otherwise `None`.
	#[must_use]
	fn f64(&self) -> Option<f64> {
		if self.vt() == co::VT::R8 {
			Some(f64::from_ne_bytes(self.raw()[..8].try_into().unwrap()))
		} else {
			None
		}
	}

	/// Creates a new object holding an `i8` value.
	#[must_use]
	fn new_i8(val: i8) -> Self
		where Self: Sized,
	{
		unsafe { Self::from_raw(co::VT::I1, &val.to_ne_bytes()) }
	}

	/// If the object holds an `i8` value, returns it, otherwise `None`.
	#[must_use]
	fn i8(&self) -> Option<i8> {
		if self.vt() == co::VT::I1 {
			Some(i8::from_ne_bytes(self.raw()[..1].try_into().unwrap()))
		} else {
			None
		}
	}

	/// Creates a new object holding an `i16` value.
	#[must_use]
	fn new_i16(val: i16) -> Self
		where Self: Sized,
	{
		unsafe { Self::from_raw(co::VT::I2, &val.to_ne_bytes()) }
	}

	/// If the object holds an `i16` value, returns it, otherwise `None`.
	#[must_use]
	fn i16(&self) -> Option<i16> {
		if self.vt() == co::VT::I2 {
			Some(i16::from_ne_bytes(self.raw()[..2].try_into().unwrap()))
		} else {
			None
		}
	}

	/// Creates a new object holding an `i32` value.
	#[must_use]
	fn new_i32(val: i32) -> Self
		where Self: Sized,
	{
		unsafe { Self::from_raw(co::VT::I4, &val.to_ne_bytes()) }
	}

	/// If the object holds an `i32` value, returns it, otherwise `None`.
	#[must_use]
	fn i32(&self) -> Option<i32> {
		if self.vt() == co::VT::I4 {
			Some(i32::from_ne_bytes(self.raw()[..4].try_into().unwrap()))
		} else {
			None
		}
	}

	/// Creates a new object holding a date/time value.
	#[must_use]
	fn new_time(val: &SYSTEMTIME) -> SysResult<Self>
		where Self: Sized,
	{
		let double = SystemTimeToVariantTime(val)?;
		Ok(unsafe { Self::from_raw(co::VT::DATE, &double.to_ne_bytes()) })
	}

	/// If the object holds a date/time value, returns it, otherwise `None`.
	#[must_use]
	fn time(&self) -> Option<SYSTEMTIME> {
		if self.vt() == co::VT::DATE {
			let double = f64::from_ne_bytes(self.raw()[..8].try_into().unwrap());
			Some(VariantTimeToSystemTime(double).unwrap())
		} else {
			None
		}
	}

	/// Creates a new object holding an `u8` value.
	#[must_use]
	fn new_u8(val: u8) -> Self
		where Self: Sized,
	{
		unsafe { Self::from_raw(co::VT::UI1, &val.to_ne_bytes()) }
	}

	/// If the object holds an `u8` value, returns it, otherwise `None`.
	#[must_use]
	fn u8(&self) -> Option<u8> {
		if self.vt() == co::VT::UI1 {
			Some(u8::from_ne_bytes(self.raw()[..1].try_into().unwrap()))
		} else {
			None
		}
	}

	/// Creates a new object holding an `u16` value.
	#[must_use]
	fn new_u16(val: u16) -> Self
		where Self: Sized,
	{
		unsafe { Self::from_raw(co::VT::UI2, &val.to_ne_bytes()) }
	}

	/// If the object holds an `u16` value, returns it, otherwise `None`.
	#[must_use]
	fn u16(&self) -> Option<u16> {
		if self.vt() == co::VT::UI2 {
			Some(u16::from_ne_bytes(self.raw()[..2].try_into().unwrap()))
		} else {
			None
		}
	}

	/// Creates a new object holding an `u32` value.
	#[must_use]
	fn new_u32(val: u32) -> Self
		where Self: Sized,
	{
		unsafe { Self::from_raw(co::VT::UI4, &val.to_ne_bytes()) }
	}

	/// If the object holds an `u32` value, returns it, otherwise `None`.
	#[must_use]
	fn u32(&self) -> Option<u32> {
		if self.vt() == co::VT::UI4 {
			Some(u32::from_ne_bytes(self.raw()[..4].try_into().unwrap()))
		} else {
			None
		}
	}
}